<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>优雅的文本遮罩动画</title>
    <style>
        :root {
            --primary-color: #2563eb;
            --secondary-color: #1e40af;
            --text-color: #1f2937;
            --bg-color: #f9fafb;
        }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: var(--bg-color);
            margin: 0;
            color: var(--text-color);
            line-height: 1.5;
        }

        .container {
            max-width: 800px;
            padding: 2rem;
        }

        .masked-text {
            font-size: clamp(2rem, 5vw, 3.5rem);
            font-weight: 700;
            line-height: 1.2;
        }

        .line {
            display: block;
            overflow: hidden;
            margin-bottom: 0.5em;
        }

        .word {
            display: inline-block;
            position: relative;
            margin-right: 0.25em;
        }

        .word::after {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
            transform: scaleX(0);
            transform-origin: left;
            z-index: 1;
            mix-blend-mode: screen;
        }

        .char {
            position: relative;
            z-index: 2;
        }

        .btn {
            padding: 0.75rem 1.5rem;
            background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
            color: white;
            border: none;
            border-radius: 0.375rem;
            font-size: 1rem;
            font-weight: 500;
            margin-top: 2rem;
            cursor: pointer;
            transition: transform 0.2s, box-shadow 0.2s;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        }

        .btn:active {
            transform: translateY(0);
        }
    </style>
</head>
<body>
<div class="container">
    <div id="text" class="masked-text">
        现代文本遮罩动画<br>
        使用GSAP与SplitText<br>
        创造流畅视觉体验
    </div>
    <button class="btn" id="replay">重新播放动画</button>
</div>

<!-- 无需手动注册，可直接使用 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.4/gsap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gsap@3.13.0/dist/SplitText.min.js"></script>
<script>
    // 注册插件
    gsap.registerPlugin(SplitText);

    let splitInstance;
    let animationTL;

    function initMaskAnimation() {
        // 清除现有实例和动画
        if (splitInstance) splitInstance.revert();
        if (animationTL) animationTL.kill();

        // 使用SplitText.create()静态方法
        splitInstance = SplitText.create("#text", {
            type: "lines,words,chars",
            linesClass: "line",
            wordsClass: "word",
            charsClass: "char",
            onSplit: (self) => {
                // 设置初始状态
                gsap.set(".word::after", {
                    scaleX: 1
                });

                // 创建主时间轴
                animationTL = gsap.timeline({
                    defaults: {
                        ease: "power3.out",
                        duration: 1.2
                    }
                });

                // 为每行添加动画
                self.lines.forEach((line, lineIndex) => {
                    const words = line.querySelectorAll(".word");

                    // 每行单词的遮罩动画
                    animationTL.to(words, {
                        "--word-after-scale-x": 0,
                        stagger: {
                            each: 0.1,
                            from: "center"
                        },
                        onUpdate: function() {
                            words.forEach(word => {
                                const scale = gsap.getProperty(word, "--word-after-scale-x");
                                word.querySelector("::after").style.transform = `scaleX(${scale})`;
                            });
                        }
                    }, lineIndex * 0.4);

                    // 每行字符的淡入动画（延迟开始）
                    animationTL.from(line.querySelectorAll(".char"), {
                        opacity: 0,
                        y: 20,
                        stagger: 0.03,
                        duration: 0.8
                    }, lineIndex * 0.4 + 0.2);
                });
            }
        });
    }

    // 初始化动画
    document.addEventListener('DOMContentLoaded', initMaskAnimation);

    // 重新播放动画
    document.getElementById("replay").addEventListener("click", () => {
        initMaskAnimation();
    });

    // 添加键盘快捷键 (R键重新播放)
    document.addEventListener("keydown", (e) => {
        if (e.key.toLowerCase() === "r") {
            initMaskAnimation();
        }
    });
</script>
</body>
</html>